﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Poi.NET.poifs.common;

namespace Poi.NET.poifs.property
{
/**
 * Factory for turning an array of RawDataBlock instances containing
 * Proprty data into an array of proper Property objects.
 *
 * The array produced may be sparse, in that any portion of data that
 * should correspond to a Property, but which does not map to a proper
 * Property (i.e., a DirectoryProperty, DocumentProperty, or
 * RootProperty) will get mapped to a null Property in the array.
 *
 * @author Marc Johnson (mjohnson at apache dot org)
 */

class PropertyFactory
{

    // no need for an accessible constructor
    private PropertyFactory()
    {
    }

    /**
     * Convert raw data blocks to an array of Property's
     *
     * @param blocks to be converted
     *
     * @return the converted List of Property objects. May contain
     *         nulls, but will not be null
     *
     * @exception IOException if any of the blocks are empty
     */

    static List<Property> convertToProperties(ListManagedBlock [] blocks)
    {
        List<Property> properties = new List<Property>();

        for (int j = 0; j < blocks.Length; j++)
        {
            byte[] data           = blocks[ j ].getData();
            int    property_count = data.Length
                                    / POIFSConstants.PROPERTY_SIZE;
            int    offset         = 0;

            for (int k = 0; k < property_count; k++)
            {
                switch (data[ offset + PropertyConstants.PROPERTY_TYPE_OFFSET ])
                {

                    case PropertyConstants.DIRECTORY_TYPE :
                        properties
                            .Add(new DirectoryProperty(properties.Count,
                                                       data, offset));
                        break;

                    case PropertyConstants.DOCUMENT_TYPE :
                        properties.Add(new DocumentProperty(properties.Count,
                                                            data, offset));
                        break;

                    case PropertyConstants.ROOT_TYPE :
                        properties.Add(new RootProperty(properties.Count,
                                                        data, offset));
                        break;

                    default :
                        properties.Add(null);
                        break;
                }
                offset += POIFSConstants.PROPERTY_SIZE;
            }
        }
        return properties;
    }
}   // end package scope class PropertyFactory

}
